﻿//DMZ.expand = {
	var miniNav = {
		show:function(){
			$('#miniNav .cat-box').show();
			var c_b_s = $('#miniNav .cat-box .cat-box-shadow');
			var c_c = $('#miniNav .cat-box .cat-column');
			/*
			//debug
			for(var i=0,l=c_c.length;i<l;i++){
				alert(c_c.eq(i).height());
			}
			*/
			//16 表示 cat-box的子孙容器的内补丁+外补丁+左右边框的和
			//2*2*c_c.length 表示 cat-column的外补丁和
			//var getWidth = c_c.width() * c_c.length+16+2*2*c_c.length;
			var getWidth = c_c.width() +16+2*2;
			$('#miniNav .cat-box').css({
				width:getWidth+'px'
			});
			
			//closed botton bind event
			$('#miniNav .cat-box .closed a').click(function(){
				$('#miniNav .cat-box').hide();
			});
		},
		hide:function(){
			$('#miniNav .cat-box').hide();
		},
		bindEvent:function(d){
			this.hide();	//初始化下拉列表的隐藏/显示状态
			//this.show();	//debug mode
			if(typeof(d.o)==='object'){
				var o = d.o;
			}else{
				var o = $(d.o);
			}
			o.hover(this.show,this.hide);
		},//end bindEvent()
		createADs:function(d){
			/*
				d = {
					source:{
						url:'' || [],
						urlTitle:'' || [],
						imgURI:'' || [],
						imgAlt:'' || []
					},
					//如果指定了source，你可以在method中自行实现source的交互模式；如果你没有指定source，请记得在method中引用有效的source
					method:(function(){})()
				}
			*/
			if(d.source&&(d.source.constructor===Array)){
				var sADs = [];
				sADs.push('<ul id="adsbox">');
				for(var i=0,len=d.source.length;i<len;i++){
					sADs.push('<li><a href="'+d.source[i].url+'" title="'+(d.source[i].text||'51新炫舞')+'">');
					if(!d.source[i].imgUri){
						sADs.push(d.source[i].text);
					}else{
						sADs.push('<img src="'+d.source[i].imgUri+'" alt="'+(d.source[i].text||'51新炫舞')+'" />');
					}
					sADs.push('</a></li>');
				}
				sADs.push('</ul>');
				sADs.join('');
				$(sADs.join('')).appendTo('#miniNav .miniNav-ads');
			}
			if(d.method && typeof(d.method)==='function'){
				d.method();
			}
			
		},//end createADs()
		createTopBar:function(d){
			/*
				@arguments 'd'
				@format 'json'
				@d.site	: {logo:'site logo class name',name:'site name',title:'site title',url:'site url'}
				@d.config	: {flag:'',order:''}
				@d.config.flag	: normal | add | cover
				@d.config.order	: desc || asc	//列表项排序
				@d.cat	:array []
				@d.cat[0]	: {title:'',items:[]}
				@d.cat[0].items[0]	:	{url:'',text:'',iconClass:className}
				@example 
					d = {
						site:{logo:'dmz',name:'51新炫舞',title:'新玩法，老朋友，新炫舞',url:'http://x5.51.com'}
						config:{flag:'add',order:'desc'},
						cat:[
							{title:'棋牌游戏',items:[{url:'http://game.51.com',text:'51游戏',iconClass:'game'}]},
							{title:'大型游戏',
							items:[
								{url:'http://x5.51.com',text:'51新炫舞',iconClass:'dmz'},
								{url:'http://wow.163.com',text:'山口山'},
								{url:'http://x5.51.com',text:'永恒之塔'}
								]
							},
							{title:'web游戏',
							items:[
								{url:'http://maoyouji.51.com',text:'猫游记'},
								{url:'http://www.joypark.com.tw',text:'轩辕剑'}
								]
							}
						]//end cat
					}
			*/
			var site = d.site;
			var shtml = [];
			var operate = {
				normal:function(){
					/**shtml.push('\t<div class="cat-column">\n');
					shtml.push('<h4>棋牌游戏</h4>\n<ul>');
					shtml.push('\n\t<li><a href="http://game.51.com" target="_blank" class="game">51游戏</a></li>');
					shtml.push('\n</ul>\n<h4>大型游戏</h4>\n<ul>');
					shtml.push('\n\t<li><a href="http://x5.51.com" target="_blank" class="dmz">51新炫舞</a></li>');
					shtml.push('\n</ul>\n\t</div>');**/
					return shtml.join('');
				},//end normal
				cover:function(d){
					var cat = d.cat;
					var catLen = cat.length;
					var shtml = [];
					var t = 0;	//位置标志，记录当前位置
					
					//快速排序方法
					var quickSort = function(arr,order){
						if(order=='desc'){
							var swap = function(a,b){ return (b.items.length-a.items.length);};
						}else{
							var swap = function(a,b){ return (a.items.length-b.items.length);};
						}
						return arr.sort(swap);
					}
					//对items进行排序
					cat = quickSort(cat,d.config.order);
					
					for(var i = 0;i<catLen;i++){
						var l = cat[i].items.length;
						
						//如果i的初始值和t一样，那么是第一次循环，将正常的输出容器的开始部分
						if(i==t){
							shtml.push('\t<div class="cat-column">\n<h4>'+cat[i].title+'</h4>\n<ul>');
						}else{
							//在第N次循环时，因为i的累加，进入新的循环， 所以i!=t，将i赋于t，标志为当前循环，表示上一轮循环已经结束
							t=i;
							//由于上一轮循环结束，所以输出上一轮循环中，容器的结束部分
							shtml.push('\n</ul>\n</div>');
							//并且输出本次循环的容器开始部分
							shtml.push('\t<div class="cat-column">\n<h4>'+cat[i].title+'</h4>\n<ul>');
						}
						for(var j=0;j<l;j++){
							shtml.push('\n\t<li><a href="'+cat[i].items[j].url+'" class="'+(cat[i].items[j].iconClass||'normal')+'" target="_blank">'+cat[i].items[j].text+'</a></li>');
						}
					}
					//由于i累加的原因，循环正常退出，n+1（此时n==ITLength）轮循环不会发生，
					//因此没法给最后一轮循环的循环体中输出窗口的结束部分
					//所以在循环结束后，补上最后的容器结束部分
					shtml.push('\n</ul>\n\t</div>');
					return shtml.join('');
				},//end cover
				activity:function(){
					shtml.push('<li class="operate">\n');
					shtml.push('\t<a href="http://www.xx5.com/" target="_blank">进入官网</a>\n');
					shtml.push('\t<a href="http://www.xx5.com/download/download-game.htm" target="_blank">游戏下载</a>\n');
					shtml.push('\t<a href="http://www.xx5.com/news_list-3-1.html" target="_blank" class="nobor">官方活动</a>\n');
					// shtml.push('\t<a href="http://www.xx5.com/bbs/" class="nobor" target="_blank">玩家论坛</a>\n');
					shtml.push('</li>\n');
					return shtml.join('');
				}//end activity
			};//end operate()

				var _miniNav = [];
				_miniNav.push('<div id="miniNav">');
				_miniNav.push('	<div class="miniNav-skin">');
				_miniNav.push('		<div class="miniNav-logo">');
				_miniNav.push('			<h2><a href="'+(site.url||'http://x5.51.com')+'" title="'+(site.title||'新玩法，老朋友，新炫舞')+'" class="'+(site.logo||'dmz')+'" target="_blank">'+(site.name||'51新炫舞')+'</a></h2>');
				_miniNav.push('		</div>');
				_miniNav.push('		<div class="miniNav-ads" id="actnav"><a href="http://www.xx5.com/index.php?a=diypage&name=topic" target="_blank" title="51新炫舞 大家来寻宝吧"><img src="http://static.51img1.com/game/common/topbar/images/ads_topbar_101228_00.jpg?v=1408050906"></a></div>');
				_miniNav.push('		<div class="object-list">');
				/**_miniNav.push('			<h3><a name="game51">51游戏</a></h3>');
				_miniNav.push('			<!-- cat-box -->');
				_miniNav.push('			<div class="cat-box">');
				_miniNav.push('				<div class="roundedBox roundedBoxTop">');
				_miniNav.push('					<div class="roundedBoxRight">');
				_miniNav.push('						<div class="roundedBoxBground"></div>');
				_miniNav.push('					</div>');
				_miniNav.push('				</div>');
				_miniNav.push('				<div class="cat-box-shadow">');
				_miniNav.push('					<div class="cat-box-skin">');
				//_miniNav.push('						<div class="cat-column">');**/
				if(d){
				switch(d.config.flag){
					case 'cover':
						_miniNav.push(operate.cover(d));
						break;
					case 'add':
						//alert('接口还未放出');
						_miniNav.push(operate.normal());
						break;
					default:
						_miniNav.push(operate.normal());
						break;
				}
				
				}else{
				operate.normal();
				}
				//_miniNav.push(shtml.join(''));
				//_miniNav.push('						</div>');
				/**_miniNav.push('					</div>');
				_miniNav.push('					<div class="closed"><a href="javascript:void(0);" title="关闭">关闭</a></div>');
				
				_miniNav.push('				</div>');
				_miniNav.push('				<div class="roundedBox roundedBoxBottom">');
				_miniNav.push('					<div class="roundedBoxRight">');
				_miniNav.push('						<div class="roundedBoxBground"></div>');
				_miniNav.push('					</div>');
				_miniNav.push('				</div>');
				_miniNav.push('			</div>');
				_miniNav.push('			<!-- //cat-box -->');**/
				_miniNav.push('		</div>');
				_miniNav.push('		<ul class="miniNav-ustate">');
				_miniNav.push('			<li>请先<a href="###" rel="miniNav-login" target="_blank">登录</a>或者<a href="###" rel="miniNav-reg" target="_blank">注册</a></li>');
				_miniNav.push('			<li>您好，<span>***</span><a href="###" rel="miniNav-exit" target="_blank">退出</a></li>');
				if(d.config.flag == 'activity')//by yangaq
				{
					_miniNav.push(operate.activity());
				}
				else
				{
					_miniNav.push('			<li><a href="http://www.xx5.com/common-get_controlpanel.html" target="_blank" >帐号管理</a></li>');
				}
				_miniNav.push('\t<li><a href="http://pay.xx5.com/" target="_blank" class="btn_pay"></a>\n</li>\n');
				_miniNav.push('		</ul>');
				_miniNav.push('	</div>');
				_miniNav.push('</div>');
			$(_miniNav.join('')).insertBefore($('body').children().eq(0));
			if(d.config.flag == 'activity')//by yangaq 活动导航条
			{
				$('#miniNav').addClass('actTopNav');
				$('#actnav').html('<a href="http://www.xx5.com/" target="_blank" title="51新炫舞 大家来寻宝吧"><img src="http://static.51img1.com/game/common/topbar/images/ads_topbar_101228_01.jpg?v=1408050906"></a>');
			}
			else if(d.config.flag == 'promoter')//by helen 推广员，和x5官网一致，但没有bgAdCommonNav
			{
				//do nothing,the same as x5
			}
			else if(d.config.flag == 'x5index')	//by zhoug	官网超首
			{
				//do nothing
			}
			else if(d.config.flag == 'x5default')	//by zhoug	官网首页
			{
				//nothing
			}
			else
			{
				//do nothing
			}
		},//end createTopBar()
		userState:function(d){
			/*
			@arguments 'd'
			@format : json
			@{u:'',url:{login:'',reg:'',exit:''},fn:function(){}}
			*/
			var uState = $('.miniNav-ustate li');
			var signin = uState.eq(0);
			var signout = uState.eq(1)
			if(d){
				var url = d.url||{login:'http://www.xx5.com',reg:'http://passport.xx5.com/register/',exit:'http://passport.xx5.com/?a=logout'};
				var state = d.u?true:false;
				if(d.fn && typeof(d.fn)==='function'){
					d.fn();
				}
			}else{
				var url = {login:'http://www.xx5.com',reg:'http://passport.xx5.com/register/',exit:'http://passport.xx5.com/?a=logout'};
				var state = false;
			}
			if(state){
				signin.hide();
				signout.show().children('span').text(d.u);
				signout.children('a[rel=miniNav-exit]').attr('href',url.exit);
			}else{
				signout.hide();
				signin.show();
				signin.children('a[rel=miniNav-login]').attr('href',url.login);
				signin.children('a[rel=miniNav-reg]').attr('href',url.reg);
			}
		},//end userState()
		init:function(d){
			/*
				@arguments 'd'
				@format 'json'
				@d.bindObject	: object or object id(use jquery mode)
				@d.createADs	: {};please view this.createADs() method
				@d.createTopBar	:{} ;please view this.createTopBar() method
				@example
					init({bindObject:object,createADs:{},createTopBar:{});						
			*/
			if(!d) return;
			this.createTopBar(d.createTopBar);
			//init this.bindEvent();
			if(!d.bindObject){
				this.bindEvent({o:'#miniNav .object-list'});
			}else{
				this.bindEvent({o:d.bindObject});
			}
			//init this.createADs();
			if(d.createADs){
				this.createADs(d.createADs);
			}
			
			//init user state
			//this.userState({u:'jeffery'});
		}//end init()
	}//end mininav()
//};